package com.itboye.sunsun.person.adapter;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.OrderQueRenDingDan;
import com.itboye.sunsun.beans.OrderBean.InnerDetailBean;
import com.itboye.sunsun.buy.ui.QueRenZhiFuActivity;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.Sha1Md5Util;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsun.volley.MyJsonRequest.Builder;

import android.accounts.Account;
import android.accounts.OnAccountsUpdateListener;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PayActivityAdapter extends BaseAdapter {

	
	private List<InnerDetailBean>list;
	private Context context;
	private String id;
	
	
	 public PayActivityAdapter(List<InnerDetailBean>list,Context context,String id) {
		this.list=list;
		this.context=context;
		this.id=id;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return  list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		
		ViewHolder holder=null;
		if(convertView==null)
		{
			holder=new ViewHolder();
			convertView=LayoutInflater.from(context).inflate(R.layout.listview_item_payactivityadapter, null);

			holder.goods = (ImageView) convertView.findViewById(R.id.goods);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.count = (TextView) convertView.findViewById(R.id.count);
			holder.guige = (TextView) convertView.findViewById(R.id.guige);
			holder.price = (TextView) convertView.findViewById(R.id.price);
			holder.payrela = (RelativeLayout) convertView.findViewById(R.id.payrela);
			holder.pay=(Button) convertView.findViewById(R.id.pay);
			convertView.setTag(holder);
		}else
		{
			holder=(ViewHolder) convertView.getTag();
		}
		holder.name.setText(list.get(position).getName());
		holder.guige.setText(list.get(position).getSkuDesc());
		holder.price.setText(list.get(position).getPrice());
		System.out.println(">>>>>>>>>>>>>>>>>>>>>"+list.get(position).getPrice());
		XImageLoader.load(list.get(position).getImgUrl(), holder.goods);
		holder.count.setText("x" + list.get(position).getCount());
		
		
		
		if(position==(list.size()-1))
		{
			holder.payrela.setVisibility(View.VISIBLE);
		}else
		{
			holder.payrela.setVisibility(View.GONE);
		}
		
		holder.pay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tijiaoDingdan(id);
				
			}
		});
		
		return convertView;
	}
	final class ViewHolder
	{
		ImageView goods;

		RelativeLayout payrela;
		Button pay;
		TextView name;
		TextView count;
		TextView guige;
		TextView price;
	}
	
	//支付订单
		private void tijiaoDingdan(String  orderCode) {
			Builder<OrderQueRenDingDan>builder=new MyJsonRequest.Builder<OrderQueRenDingDan>()
					.apiVer("100")
					.typeKey("BY_Orders_pay")
					.param("uid", SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
					 .param("order_id", orderCode)

					
					.requestListener(new XRequestListener<OrderQueRenDingDan>() {
						
						@Override
						public void onResponse(OrderQueRenDingDan arg0) {
							 
							String name=arg0.getName();
							String desc=arg0.getDesc();
							
							name=URLDecoder.decode(name);
							desc=URLDecoder.decode(desc);
							
							
							String str=arg0.getCode()+name+desc+arg0.getTotalPrice();
							
							str=URLEncoder.encode(str);
							
							str=str.toLowerCase();
							
							 
							
							str=Sha1Md5Util.SHA1(Sha1Md5Util.MD5(str));
							
							str=str+"ITBOYE_PAY_DASFSAFKL";
							
							str=Sha1Md5Util.MD5(str);
							
							if (arg0.getKey().equals(str)) {
								Intent intent=new Intent(App.ctx, QueRenZhiFuActivity.class);
								intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								intent.putExtra("what", arg0);
								App.ctx.startActivity(intent);
							}else{
								Toast.makeText(App.ctx, "订单异常", 1).show();
							}
							
							// DebugToast.show("付款。。。", 1);
							
						}
					})
					.errorListener(new XErrorListener() {
						
						@Override
						public void onErrorResponse(Exception exception, int code, String msg) {
							
						}
					});
			
//			if (youhuiquanId!=null) {
//				builder.param("idcode", youhuiquanId);
//			}
//			
			HttpRequest.getDefaultRequestQueue().add(builder.build());
			
		}

}
